/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;
import model.Speler;
import components.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;


/**
 *
 * @author tomkeim
 * 
 * 
 * Masterclass (Masterclassid, leraar, kosten, minmale_rating)
 * 
 */
public class Masterclass {
    private int masterclassID;
    private Speler leraar;
    private double kosten;
    private String datumTijd;
    private int minimaleRating;

    public String getDatumTijd() {
        return datumTijd;
    }

    public void setDatumTijd(String datumTijd) {
        this.datumTijd = datumTijd;
    }

    public double getKosten() {
        return kosten;
    }

    public void setKosten(double kosten) {
        this.kosten = kosten;
    }

    public Speler getLeraar() {
        return leraar;
    }

    public void setLeraar(Speler leraar) {
        this.leraar = leraar;
    }

    public int getMasterclassID() {
        return masterclassID;
    }

    public void setMasterclassID(int masterclassID) {
        this.masterclassID = masterclassID;
    }

    public int getMinimaleRating() {
        return minimaleRating;
    }

    public void setMinimaleRating(int minimaleRating) {
        this.minimaleRating = minimaleRating;
    }
    

      public static Masterclass[] getMasterclasses() {
        ArrayList<Masterclass> masterclasses = new ArrayList<Masterclass>();
        Masterclass newMasterclass;
        String query = "SELECT * FROM Masterclass";
        ResultSet result = Database.executeSelectQuery(query);
        try {
            while(result.next()) {
                newMasterclass = new Masterclass();
                
                newMasterclass.setMasterclassID(result.getInt("masterclassid"));
                Speler leraar = Speler.getSpeler(result.getInt("leraar"));
                newMasterclass.setLeraar(leraar);
                newMasterclass.setDatumTijd(result.getString("datum_tijd"));
                newMasterclass.setKosten(result.getDouble("kosten"));
                newMasterclass.setMinimaleRating(result.getInt("minimale_rating"));
                
                masterclasses.add(newMasterclass);
            }
        } catch(SQLException e) {
            System.out.println(e.getMessage());
        }
        Masterclass[] masterclassArray = new Masterclass[masterclasses.size()];
        for(int i=0;i<masterclasses.size();i++) {
            masterclassArray[i] = masterclasses.get(i);
        }
        
        return masterclassArray;
    }
    
      public static boolean exists(int id) {
           boolean count = false;
        Masterclass newMasterclass = new Masterclass();
        
        String query = "SELECT * FROM Masterclass WHERE masterclassid = " + id;
        
        ResultSet result = Database.executeSelectQuery(query);
        
        try {
            result.next();
           
            newMasterclass.setMasterclassID(result.getInt("masterclassid"));
            Speler leraar = Speler.getSpeler(result.getInt("leraar"));
            newMasterclass.setLeraar(leraar);
            newMasterclass.setDatumTijd(result.getString("datum_tijd"));
            newMasterclass.setKosten(result.getDouble("kosten"));
            newMasterclass.setMinimaleRating(result.getInt("minimale_rating"));
            count = true;
            
        } catch(SQLException e) {
            //Error afhandeling
        }
        
        // Note by : Ik weet  dat dit waarschijnlijk een ver van perfecte methode is, maar we hebben alvast iets nu. Suggesties zijn uiteraard welkom.
        return count;
            }
      
      
    public static Masterclass getMasterclass(int id) {
        
        
        Masterclass newMasterclass = new Masterclass();
        
        String query = "SELECT * FROM Masterclass WHERE masterclassid = " + id;
        
        ResultSet result = Database.executeSelectQuery(query);
        
        try {
            result.next();
           
            newMasterclass.setMasterclassID(result.getInt("masterclassid"));
            Speler leraar = Speler.getSpeler(result.getInt("leraar"));
            newMasterclass.setLeraar(leraar);
            newMasterclass.setDatumTijd(result.getString("datum_tijd"));
            newMasterclass.setKosten(result.getDouble("kosten"));
            newMasterclass.setMinimaleRating(result.getInt("minimale_rating"));
            
            
        } catch(SQLException e) {
            //Error afhandeling
        }
        
        
        return newMasterclass;
            }
    
         public boolean insert() {
    boolean successful = false;
      //sla dit Docent object op in de database, 
      //mbv  executeInsertQuery uit componenten.Database
     
      String query = "Insert into Masterclass (leraar,datum_tijd,kosten,minimale_rating) values('" +
              this.getLeraar().getSpelerid() + "', '" +
              this.getDatumTijd() + "', " +
              this.getKosten() + ", '" +
              this.getMinimaleRating() + "')";
              
      
        if (Database.executeInsertQuery(query) > 0) { 
            //tenminste 1 record inserted
              successful = true;
        }
    return successful;
  }
    
   
   
          
           //update student in de database op basis van zijn naam
  public boolean update() {
   // if (alleVeldenIngevuld()) {
      String query = "update Masterclass set " +
              
              "leraar = '" + leraar.getSpelerid() + "', " +
              "kosten = " + kosten + ", " +
              "datum_tijd ='" + datumTijd + "', " +
              "minimale_rating = " + minimaleRating + " where masterclassid = " + this.masterclassID;
    if (Database.executeUpdateQuery(query) == 1) return true;
       
  return false;
}

         
         
         
         
    public boolean delete() {
        boolean successful = false;
        String query = "delete from Masterclass where masterclassid ='" + this.masterclassID + "'";
    if (Database.executeDeleteQuery(query) == 1){
        // er is 1 record verwijderd
        successful = true;
    }
    return successful;
    }
    
    
    
    
    
    
    
    @Override
    public String toString() {
        String output = "#" + this.getMasterclassID() + " op " + this.getDatumTijd();
        return output;
    }


}
